<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class m_preferences extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    //insert data
    function insert($params) {
        $sql = "INSERT INTO com_preferences (pref_group, pref_nm, pref_value, mdb, mdd)
            VALUES (?, ?, ?, ?, NOW())";
        return $this->db->query($sql, $params);
    }

    //update data
    function update($params) {
        $sql = "UPDATE com_preferences SET pref_value = ?, mdb = ?, mdd = NOW() WHERE pref_id = ?";
        return $this->db->query($sql, $params);
    }

    //delete data
    function delete($params) {
        $sql = "DELETE FROM com_preferences WHERE pref_id = ?";
        return $this->db->query($sql, $params);
    }

    // get all group preferences
    function get_all_group() {
        $sql = "SELECT pref_group FROM com_preferences GROUP BY pref_group ORDER BY pref_group ASC";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    // get all data
    function get_all_preferences_by_group($params) {
        $sql = "SELECT * FROM com_preferences WHERE pref_group LIKE ? ORDER BY pref_group ASC, pref_nm ASC";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    //get all data
    function get_all_preferences() {
        $sql = "SELECT * FROM com_preferences ";
        $query = $this->db->query($sql);

        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    //get by id for searching
    function get_preferences_by_id($id) {
        $sql = "SELECT * FROM com_preferences WHERE pref_id = ?";
        $query = $this->db->query($sql, $id);

        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    //get total data
    function get_total_preferences() {
        $sql = "SELECT COUNT(*)'total' FROM com_preferences";
        $query = $this->db->query($sql);

        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return 0;
        }
    }

    //get all data for pagination
    function get_all_preferences_limit($params) {
        $sql = "SELECT * FROM com_preferences 
            ORDER BY pref_id LIMIT ?, ?";
        $query = $this->db->query($sql, $params);

        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    //get unique group preferences miftah
    function get_all_group_preferences($params) {
        $sql = "SELECT DISTINCT pref_group FROM com_preferences 
            WHERE pref_group LIKE ? ORDER BY pref_group ASC";
        $query = $this->db->query($sql, $params);

        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_preferences_by_group_and_name($params) {
        $sql = "SELECT * FROM com_preferences WHERE pref_group = ? AND pref_nm = ?";
        $query = $this->db->query($sql, $params);

        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

}